package com.lv.service.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.blankj.utilcode.util.LogUtils
import com.lv.common.imp.DecimalInputTextWatcher
import com.lv.common.imp.OnPositionTListener
import com.lv.common.utils.MoneyUtil
import com.lv.common.utils.showToast
import com.lv.service.R
import com.lxj.xpopup.impl.PartShadowPopupView

/**
 * 作者： ygx
 * 创建日期：2022/10/10 10:32 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：底部公共list选择器
 */
class PriceEditorPopupView(
    private val mContext: Context,
    private val mAllBeans: List<Int>? = null,
    private val mTitle: String,
    private val topH: Int = 0,
    private val mLayout: Int = R.layout.price_editor_popup,
    private val mPositionTListener: OnPositionTListener<List<Int>>? = null
) :
    PartShadowPopupView(mContext) {
    private var current = -1
    private var currentTemp = -1
    private val name = ""
    private var tvTitle: TextView? = null
    private var mEtPrice0: EditText? = null
    private var mEtPrice1: EditText? = null
    private var mEtPrice2: EditText? = null
    private var mEtPrice3: EditText? = null
    private var mEtPrice4: EditText? = null
    private var mEtPrice5: EditText? = null
    private var mEtPrice6: EditText? = null
    private var mEtPrice7: EditText? = null
    private var viewTop: View? = null
    private var viewShadow: TextView? = null
    private var ivClose: ImageView? = null
    private val mBeans = mutableListOf<Int>()

    override fun getImplLayoutId(): Int {
        return mLayout
    }

    override fun onCreate() {
        super.onCreate()
        tvTitle = findViewById<TextView>(R.id.tv_title)
        viewTop = findViewById<View>(R.id.view_top)
        viewShadow = findViewById<TextView>(R.id.tv_submit)
        ivClose = findViewById<ImageView>(R.id.iv_close)
        mEtPrice0 = findViewById<EditText>(R.id.et_price_0)
        mEtPrice1 = findViewById<EditText>(R.id.et_price_1)
        mEtPrice2 = findViewById<EditText>(R.id.et_price_2)
        mEtPrice3 = findViewById<EditText>(R.id.et_price_3)
        mEtPrice4 = findViewById<EditText>(R.id.et_price_4)
        mEtPrice5 = findViewById<EditText>(R.id.et_price_5)
        mEtPrice6 = findViewById<EditText>(R.id.et_price_6)
        mEtPrice7 = findViewById<EditText>(R.id.et_price_7)
        val layoutParams = viewTop?.layoutParams as ConstraintLayout.LayoutParams
        layoutParams.height = topH
        initView()
        initEvnet()
    }

    @SuppressLint("SetTextI18n")
    private fun initView() {
        mEtPrice0?.setOnLongClickListener { true }
        mEtPrice1?.setOnLongClickListener { true }
        mEtPrice2?.setOnLongClickListener { true }
        mEtPrice3?.setOnLongClickListener { true }
        mEtPrice4?.setOnLongClickListener { true }
        mEtPrice5?.setOnLongClickListener { true }
        mEtPrice6?.setOnLongClickListener { true }
        mEtPrice7?.setOnLongClickListener { true }
        mEtPrice0?.addTextChangedListener(DecimalInputTextWatcher(mEtPrice0, 7, 2))
        mEtPrice1?.addTextChangedListener(DecimalInputTextWatcher(mEtPrice1, 7, 2))
        mEtPrice2?.addTextChangedListener(DecimalInputTextWatcher(mEtPrice2, 7, 2))
        mEtPrice3?.addTextChangedListener(DecimalInputTextWatcher(mEtPrice3, 7, 2))
        mEtPrice4?.addTextChangedListener(DecimalInputTextWatcher(mEtPrice4, 7, 2))
        mEtPrice5?.addTextChangedListener(DecimalInputTextWatcher(mEtPrice5, 7, 2))
        mEtPrice6?.addTextChangedListener(DecimalInputTextWatcher(mEtPrice6, 7, 2))
        mEtPrice7?.addTextChangedListener(DecimalInputTextWatcher(mEtPrice7, 7, 2))

        if (!mAllBeans.isNullOrEmpty()) {

            if (mAllBeans.size > 1) {
                val price1 = MoneyUtil.moneydiv("${mAllBeans[1]}", "100")
                mEtPrice1?.setText(price1)
//                mEtPrice1?.setSelection(price1.length)
            }
            if (mAllBeans.size > 2) {
                val price2 = MoneyUtil.moneydiv("${mAllBeans[2]}", "100")
                mEtPrice2?.setText(price2)
//                mEtPrice2?.setSelection(price2.length)
            }
            if (mAllBeans.size > 3) {
                val price3 = MoneyUtil.moneydiv("${mAllBeans[3]}", "100")
                mEtPrice3?.setText(price3)
//                mEtPrice3?.setSelection(price3.length)
            }
            if (mAllBeans.size > 4) {
                val price4 = MoneyUtil.moneydiv("${mAllBeans[4]}", "100")
                mEtPrice4?.setText(price4)
//                mEtPrice4?.setSelection(price4.length)
            }
            if (mAllBeans.size > 5) {
                val price5 = MoneyUtil.moneydiv("${mAllBeans[5]}", "100")

                mEtPrice5?.setText(price5)
//                mEtPrice5?.setSelection(price5.length)
            }
            if (mAllBeans.size > 6) {
                val price6 = MoneyUtil.moneydiv("${mAllBeans[6]}", "100")
                mEtPrice6?.setText(price6)
//                mEtPrice6?.setSelection(price6.length)
            }
            if (mAllBeans.size > 7) {
                val price7 = MoneyUtil.moneydiv("${mAllBeans[7]}", "100")
                mEtPrice7?.setText(price7)
//                mEtPrice7?.setSelection(price7.length)
            }
            val price0 = MoneyUtil.moneydiv("${mAllBeans[0]}", "100")
//            val price0="0.00"
            mEtPrice0?.setText(price0)
            val text = mEtPrice0?.text.toString()
            mEtPrice0?.setSelection(text.length)

        }
        tvTitle?.text = mTitle


    }

    private fun initEvnet() {

        viewTop?.setOnClickListener {

            dismiss()
        }
        ivClose?.setOnClickListener {

            dismiss()
        }

        viewShadow?.setOnClickListener {
            val price0 = mEtPrice0?.text?.trim()
            if (price0.isNullOrEmpty() || !MoneyUtil.moneyComp("$price0", "0.01")) {
                showToast("金额1输入有误，请重新输入")
                return@setOnClickListener
            }
            val price1 = mEtPrice1?.text?.trim()
            if (price1.isNullOrEmpty() || !MoneyUtil.moneyComp("$price1", "0.01")) {
                showToast("金额2输入有误，请重新输入")
                return@setOnClickListener
            }
            val price2 = mEtPrice2?.text?.trim()
            if (price2.isNullOrEmpty() || !MoneyUtil.moneyComp("$price2", "0.01")) {
                showToast("金额3输入有误，请重新输入")
                return@setOnClickListener
            }
            val price3 = mEtPrice3?.text?.trim()
            if (price3.isNullOrEmpty() || !MoneyUtil.moneyComp("$price3", "0.01")) {
                showToast("金额4输入有误，请重新输入")
                return@setOnClickListener
            }
            val price4 = mEtPrice4?.text?.trim()
            if (price4.isNullOrEmpty() || !MoneyUtil.moneyComp("$price4", "0.01")) {
                showToast("金额5输入有误，请重新输入")
                return@setOnClickListener
            }
            val price5 = mEtPrice5?.text?.trim()
            if (price5.isNullOrEmpty() || !MoneyUtil.moneyComp("$price5", "0.01")) {
                showToast("金额6输入有误，请重新输入")
                return@setOnClickListener
            }
            val price6 = mEtPrice6?.text?.trim()
            if (price6.isNullOrEmpty() || !MoneyUtil.moneyComp("$price6", "0.01")) {
                showToast("金额7输入有误，请重新输入")
                return@setOnClickListener
            }
            val price7 = mEtPrice7?.text?.trim()
            if (price7.isNullOrEmpty() || !MoneyUtil.moneyComp("$price7", "0.01")) {
                showToast("金额8输入有误，请重新输入")
                return@setOnClickListener
            }
            val p0 = MoneyUtil.moneyMul("$price0", "100").toDouble().toInt()
            val p1 = MoneyUtil.moneyMul("$price1", "100").toDouble().toInt()
            val p2 = MoneyUtil.moneyMul("$price2", "100").toDouble().toInt()
            val p3 = MoneyUtil.moneyMul("$price3", "100").toDouble().toInt()
            val p4 = MoneyUtil.moneyMul("$price4", "100").toDouble().toInt()
            val p5 = MoneyUtil.moneyMul("$price5", "100").toDouble().toInt()
            val p6 = MoneyUtil.moneyMul("$price6", "100").toDouble().toInt()
            val p7 = MoneyUtil.moneyMul("$price7", "100").toDouble().toInt()
            mBeans.clear()
            mBeans.add(p0)
            mBeans.add(p1)
            mBeans.add(p2)
            mBeans.add(p3)
            mBeans.add(p4)
            mBeans.add(p5)
            mBeans.add(p6)
            mBeans.add(p7)
            val list = mBeans.distinct()
            if (list.size == 8) {
                mPositionTListener?.onClick(current, list)
            } else {
                showToast("金额不能重复，请重新输入")
                return@setOnClickListener
            }

            dismiss()
        }
    }

    private fun setDismissDialog() {
        dismiss()
    }


    override fun onDestroy() {
        super.onDestroy()
    }

    override fun beforeDismiss() {
        super.beforeDismiss()


    }

}